package com.springapi.controller;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ReUtil;
import com.springapi.entity.Setting;
import com.springapi.service.SettingService;
import com.springapi.utils.HttpServiceHutool;
import com.springapi.utils.MailfileCheckerUtil;
import com.springapi.utils.Myutil;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/umiocr")
public class UmiocrController {


    @Resource
    private SettingService settingService;

    //快瞳ocr运单号识别，注意加密手机号无法识别完整手机号
    @PostMapping("/ocr")
    public ResponseEntity<Map<String, Object>> umiocrocr(@RequestParam("file") MultipartFile file) {

        // 创建一个Map来存储结果
        Map<String, Object> resultMap = new HashMap<>();


        String ocrapi="http://127.0.0.1:1224";
        /*
        List<Setting>  sets=settingService.findshopsetting(0,0);
        if(sets.isEmpty()){
            System.out.println("未配置umiocr加速api");
        }else{
            Setting shopsetting = sets.get(0);
            String setvlaue=shopsetting.getValue();
            if(setvlaue.isEmpty()){
                System.out.println("umiocr加速api地址为空");
            }else{
                System.out.println("开启ocr加速服务");
                ocrapi=setvlaue;
            }
        }
        */
        //ocr地址
        String url = ocrapi+"/api/ocr";
        System.out.println("ocr当前请求地址："+url);
        String encodedFile = null;
        try {
            byte[] fileBytes = file.getBytes();
            encodedFile=  Base64Utils.encodeToString(fileBytes);
           // System.out.println("encodedFile："+encodedFile);
            //设置请求头
            Map<String, String> headers = new HashMap<>();
            //headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            Map<String, Object> params = new HashMap<>();
            params.put("base64", encodedFile);
            HttpServiceHutool h = new HttpServiceHutool();
            Map<String, Object>  mailmap= new HashMap<>();
            resultMap=h.sendPostRequestjson(url, params, headers);
            mailmap = h.sendPostRequestjson(url, params, headers);




            // 提取 data 列表
            List<?> dataList = (List<?>) mailmap.get("data");
            //System.out.println("解析数组长度："+dataList);

            boolean containsYunda = MailfileCheckerUtil.containsTextInData(resultMap, "韵达");
            System.out.println("是否是韵达："+containsYunda);

            boolean containsZhongtong = MailfileCheckerUtil.containsTextInData(resultMap, "中通");
            System.out.println("是否是中通："+containsZhongtong);

            boolean containsYuantong = MailfileCheckerUtil.containsTextInData(resultMap, "圆通");
            System.out.println("是否是圆通："+containsYuantong);

            boolean containsShentong = MailfileCheckerUtil.containsTextInData(resultMap, "申通");
            System.out.println("是否是申通："+containsShentong);

            boolean containsDebang = MailfileCheckerUtil.containsTextInData(resultMap, "德邦");
            System.out.println("是否是德邦："+containsDebang);

            boolean containsShunfeng = MailfileCheckerUtil.containsTextInData(resultMap, "顺丰");
            System.out.println("是否是顺丰："+containsShunfeng);

            boolean containsJitu = MailfileCheckerUtil.containsTextInData(resultMap, "极兔");
            System.out.println("是否是极兔："+containsJitu);

            boolean containsYouzheng = MailfileCheckerUtil.containsTextInData(resultMap, "邮政");
            System.out.println("是否是邮政："+containsYouzheng);

            if(containsYunda){
                resultMap.put("kd", "韵达");
                //韵达快递单解析
                // 检查列表是否足够长
                if (dataList != null && dataList.size() > 12) {
                    String ydh="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        // 使用正则表达式检查字符串是否由纯数字组成
                        if (texti != null && texti.matches("\\d+") && texti.length() > 12 ) {
                            ydh=texti;
                            break;
                        }

                    }

                    System.out.println("运单号："+ydh);
                    resultMap.put("ydh", ydh);

                    //查找手机号
                    String  mailphone="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        // 使用正则表达式检查字符串是否由纯数字组成
                        if (texti != null && texti.contains("转") ) {
                            mailphone=Myutil.extractPhoneNumber(texti.toString());
                            break;
                        }

                    }
                    System.out.println("手机号："+mailphone);
                    resultMap.put("mailphone", mailphone);

                    String jmphone="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        // 使用正则表达式检查字符串是否由纯数字组成
                        if (texti != null && texti.contains("*******") && texti.length() ==11 ) {
                            jmphone=texti;
                            break;
                        }

                    }
                    System.out.println("加密手机号码: " + jmphone);
                    resultMap.put("jmphone", jmphone);


                    //查找收件人
                    String mailusername="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        // 使用正则表达式检查字符串是否由纯数字组成
                        if (texti != null  && texti.length() <11 ) {
                            if(texti.contains("*") ||  texti.contains("串")){
                                mailusername=Myutil.getFirstChineseCharacter(texti)+"*";
                                break;
                            }
                        }

                    }
                    System.out.println("收件人：: " + mailusername);
                    resultMap.put("mailusername", mailusername);

                    //查找收件地址
                    String mailaddress="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        int chineseCount = 0;
                        for (char c : texti.toCharArray()) {
                            // 检查字符是否在汉字范围内
                            if (c >= '\u4e00' && c <= '\u9fff') {
                                chineseCount++;
                            }
                        }
                        // 如果汉字数量超过10个，则输出“123”
                        if (chineseCount > 10) {
                            mailaddress=texti;
                            break;
                        }


                    }

                    System.out.println("地址: " + mailaddress);
                    resultMap.put("mailaddress", mailaddress);

                    String mailmassage="韵达快递，"+"运单号:"+ydh.toString()+" "+mailusername+" "+jmphone+" "+mailaddress;
                    System.out.println("详细信息: " + mailmassage);
                    resultMap.put("mailmassage", mailmassage);
                } else {
                    System.out.println("解析失败");
                }
            }else if(containsZhongtong){
                //中通快递单解析
                resultMap.put("kd", "中通");
                // 检查列表是否足够长
                if (dataList != null && dataList.size() > 12) {
                    String ydh="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        // 使用正则表达式检查字符串是否由纯数字组成
                        if (texti != null && texti.matches("\\d+") && texti.length() > 12 ) {
                            ydh=texti;
                            break;
                        }

                    }

                    System.out.println("运单号："+ydh);
                    resultMap.put("ydh", ydh);

                    //查找手机号
                    String  mailphone="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        // 使用正则表达式检查字符串是否由纯数字组成
                        if (texti != null && texti.contains("转") &&  ReUtil.contains("\\d+", texti)) {
                           // System.out.println("texti:"+texti);
                            mailphone=Myutil.extractPhoneNumber(texti.toString());
                            break;
                        }

                    }
                    System.out.println("手机号："+mailphone);
                    resultMap.put("mailphone", mailphone);

                    String jmphone="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        // 使用正则表达式检查字符串是否由纯数字组成
                        if (texti != null && texti.contains("*******") && texti.length() ==11 ) {
                            jmphone=texti;
                            break;
                        }

                    }
                    System.out.println("加密手机号码: " + jmphone);
                    resultMap.put("jmphone", jmphone);


                    //查找收件人
                    String mailusername="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        // 判断识别的元素是否包含中文，判断识别的元素小于11为防止是电话
                        if (texti != null && texti.length() <11 && Validator.hasChinese(texti)) {
                            if(texti.contains("*") ||  texti.contains("串")){
                                mailusername=Myutil.getFirstChineseCharacter(texti)+"*";
                                break;
                            }

                        }

                    }
                    System.out.println("收件人：: " + mailusername);
                    resultMap.put("mailusername", mailusername);

                    //查找收件地址
                    String mailaddress="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        int chineseCount = 0;
                        for (char c : texti.toCharArray()) {
                            // 检查字符是否在汉字范围内
                            if (c >= '\u4e00' && c <= '\u9fff') {
                                chineseCount++;
                            }
                        }
                        // 如果汉字数量超过10个，则输出“123”
                        if (chineseCount > 10) {
                            mailaddress=texti;
                            break;
                        }


                    }

                    System.out.println("地址: " + mailaddress);
                    resultMap.put("mailaddress", mailaddress);

                    String mailmassage="中通快递，"+"运单号:"+ydh.toString()+" "+mailusername+" "+jmphone+" "+mailaddress;
                    System.out.println("详细信息: " + mailmassage);
                    resultMap.put("mailmassage", mailmassage);
                } else {
                    System.out.println("解析失败");
                }
            }else if(containsYuantong){
                //圆通快递单解析
                resultMap.put("kd", "圆通");
                // 检查列表是否足够长
                if (dataList != null && dataList.size() > 12) {

                    String ydh="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();
                        //System.out.println("index:"+i+" "+"texti:"+texti+" 字符串长度:"+texti.length()+"是否包含yt："+texti.contains("YT"));
                        // 使用正则表达式检查字符串是否由纯数字组成
                        if (texti != null && texti.contains("YT") && texti.length() > 12 ) {
                            //System.out.println("符合index:"+i);
                            ydh=texti;
                            break;
                        }

                    }

                    System.out.println("运单号："+ydh);
                    resultMap.put("ydh", ydh);

                    //查找手机号
                    String  mailphone="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        // 使用正则表达式检查字符串是否由纯数字组成
                        if (texti != null  &&  Myutil.extractPhoneNumber2(texti) != null) {
                            // System.out.println("texti:"+texti);
                            mailphone=Myutil.extractPhoneNumber2(texti);
                            break;
                        }

                    }
                    System.out.println("手机号："+mailphone);
                    resultMap.put("mailphone", mailphone);

                    String jmphone=mailphone;

                    System.out.println("加密手机号码: " + jmphone);
                    resultMap.put("jmphone", jmphone);


                    //查找收件人
                    String mailusername="";


                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        // 使用正则表达式检查字符串是否由纯数字组成
                        if (texti != null  &&  Myutil.extractPhoneNumber2(texti) != null) {
                            // System.out.println("texti:"+texti);
                            mailusername=Myutil.extractNonPhoneNumberPart(texti);
                            break;
                        }

                    }
                    System.out.println("收件人：: " + mailusername);
                    resultMap.put("mailusername", mailusername);

                    //查找收件地址
                    String mailaddress="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        int chineseCount = 0;
                        for (char c : texti.toCharArray()) {
                            // 检查字符是否在汉字范围内
                            if (c >= '\u4e00' && c <= '\u9fff') {
                                chineseCount++;
                            }
                        }
                        // 如果汉字数量超过10个，则输出“123”
                        if (chineseCount > 10) {
                            mailaddress=texti;
                            break;
                        }


                    }

                    System.out.println("地址: " + mailaddress);
                    resultMap.put("mailaddress", mailaddress);

                    String mailmassage="中通快递，"+"运单号:"+ydh.toString()+" "+mailusername+" "+jmphone+" "+mailaddress;
                    System.out.println("详细信息: " + mailmassage);
                    resultMap.put("mailmassage", mailmassage);
                } else {
                    System.out.println("解析失败");
                }
            }else if(containsShentong){
                //申通快递
                resultMap.put("kd", "申通");
                // 检查列表是否足够长
                if (dataList != null && dataList.size() > 12) {
                    String ydh="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        // 使用正则表达式检查字符串是否由纯数字组成
                        if (texti != null && Myutil.checkAndOutput(texti,11) && texti.length() >= 12 ) {
                            ydh=texti;
                            break;
                        }

                    }

                    System.out.println("运单号："+ydh);
                    resultMap.put("ydh", ydh);

                    //查找手机号
                    String  mailphone="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        // 使用正则表达式检查字符串是否由纯数字组成
                        if (texti != null ) {
                             //System.out.println("texti:"+texti);
                             String ssh=Myutil.extractFirstNumber(texti);
                             //System.out.println("ssh:"+ssh+Myutil.checkAndOutput(ssh,11));
                             if(Myutil.checkAndOutput(ssh,11) && ssh.length()<12){
                                 mailphone=Myutil.extractFirstNumber(texti.toString());
                                 break;
                             }

                        }

                    }
                    System.out.println("手机号："+mailphone);
                    resultMap.put("mailphone", mailphone);

                    String jmphone="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        // 使用正则表达式检查字符串是否由纯数字组成
                        if (texti != null && texti.contains("*******") ) {
                            // System.out.println("texti:"+texti);
                            jmphone="*******"+Myutil.getstrbyxing(texti,4);
                            break;
                        }

                    }
                    System.out.println("加密手机号码: " + jmphone);
                    resultMap.put("jmphone", jmphone);


                    //查找收件人
                    String mailusername="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        // 判断识别的元素是否包含中文，判断识别的元素小于11为防止是电话
                        if (texti != null ) {
                            //System.out.println("texti:"+texti);
                            String ssh=Myutil.extractFirstNumber(texti);
                            //System.out.println("ssh:"+ssh+Myutil.checkAndOutput(ssh,11));
                            if(Myutil.checkAndOutput(ssh,11) && ssh.length()<12){
                                mailusername=Myutil.extractNonPhoneNumberPart(texti.toString());
                                break;
                            }

                        }

                    }


                    System.out.println("收件人：: " + mailusername);
                    resultMap.put("mailusername", mailusername);

                    //查找收件地址
                    String mailaddress="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        int chineseCount = 0;
                        for (char c : texti.toCharArray()) {
                            // 检查字符是否在汉字范围内
                            if (c >= '\u4e00' && c <= '\u9fff') {
                                chineseCount++;
                            }
                        }
                        // 如果汉字数量超过10个，则输出“123”
                        if (chineseCount > 10) {
                            mailaddress=texti;
                            break;
                        }


                    }

                    System.out.println("地址: " + mailaddress);
                    resultMap.put("mailaddress", mailaddress);

                    String mailmassage="申通快递，"+"运单号:"+ydh.toString()+" "+mailusername+" "+jmphone+" "+mailaddress;
                    System.out.println("详细信息: " + mailmassage);
                    resultMap.put("mailmassage", mailmassage);
                } else {
                    System.out.println("解析失败");
                }


            }else if(containsDebang){
                //德邦快递解析
                resultMap.put("kd", "德邦");
                // 检查列表是否足够长
                if (dataList != null && dataList.size() > 12) {
                    String ydh="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        // 使用正则表达式检查字符串是否由纯数字组成
                        if (texti != null && Myutil.checkAndOutput(texti,11) && texti.length() >= 12 ) {
                            ydh=texti;
                            break;
                        }

                    }

                    System.out.println("运单号："+ydh);
                    resultMap.put("ydh", ydh);

                    //查找手机号
                    String  mailphone="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        // 使用正则表达式检查字符串是否由纯数字组成
                        if (texti != null && texti.contains("转") &&  ReUtil.contains("\\d+", texti)) {
                            // System.out.println("texti:"+texti);
                            mailphone=Myutil.extractPhoneNumber(texti.toString());
                            break;
                        }

                    }
                    System.out.println("手机号："+mailphone);
                    resultMap.put("mailphone", mailphone);

                    String jmphone="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        // 使用正则表达式检查字符串是否由纯数字组成
                        if (texti != null && texti.contains("*******") ) {
                           // System.out.println("texti:"+texti);
                            jmphone="*******"+Myutil.getstrbyxing(texti,4);
                            break;
                        }

                    }
                    System.out.println("加密手机号码: " + jmphone);
                    resultMap.put("jmphone", jmphone);


                    //查找收件人
                    String mailusername="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        // 判断识别的元素是否包含中文，判断识别的元素小于11为防止是电话
                        if (texti.contains("********") ) {
                            mailusername=Myutil.getFirstChineseCharacter(texti)+"*";
                            break;

                        }

                    }


                    System.out.println("收件人：: " + mailusername);
                    resultMap.put("mailusername", mailusername);

                    //查找收件地址
                    String mailaddress="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        int chineseCount = 0;
                        for (char c : texti.toCharArray()) {
                            // 检查字符是否在汉字范围内
                            if (c >= '\u4e00' && c <= '\u9fff') {
                                chineseCount++;
                            }
                        }
                        // 如果汉字数量超过10个，则输出“123”
                        if (chineseCount > 10) {
                            mailaddress=texti;
                            break;
                        }


                    }

                    System.out.println("地址: " + mailaddress);
                    resultMap.put("mailaddress", mailaddress);

                    String mailmassage="德邦快递，"+"运单号:"+ydh.toString()+" "+mailusername+" "+jmphone+" "+mailaddress;
                    System.out.println("详细信息: " + mailmassage);
                    resultMap.put("mailmassage", mailmassage);
                } else {
                    System.out.println("解析失败");
                }


            }else if(containsShunfeng){
                //顺丰快递解析
                resultMap.put("kd", "顺丰");
                // 检查列表是否足够长
                if (dataList != null && dataList.size() > 12) {
                    String ydh="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        // 使用正则表达式检查字符串是否由纯数字组成
                        if (texti != null && Myutil.checkAndOutput(texti,11) && texti.length() >= 12 ) {
                            ydh=texti;
                            break;
                        }

                    }

                    System.out.println("运单号："+ydh);
                    resultMap.put("ydh", ydh);

                    //查找手机号


                    String jmphone="";
                    int jmphoneindex=0;
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        // 使用正则表达式检查字符串是否由纯数字组成
                        if (texti != null && texti.matches(".*\\*{4}.*") ) {
                            // System.out.println("texti:"+texti);
                            jmphone=texti;
                            jmphoneindex=i;
                            break;
                        }

                    }
                    System.out.println("加密手机号码: " + jmphone);
                    resultMap.put("jmphone", jmphone);


                    String  mailphone=jmphone;

                    System.out.println("手机号："+mailphone);
                    resultMap.put("mailphone", mailphone);

                    //查找收件人
                    String mailusername="";
                    if(jmphoneindex>0){
                        Map<?, ?> datauser = (Map<?, ?>) dataList.get(jmphoneindex-1);
                        // 提取 text 值
                        String texti = datauser.get("text").toString();
                        mailusername=Myutil.getFirstChineseCharacter(texti)+"*";
                    }

                    System.out.println("收件人：: " + mailusername);
                    resultMap.put("mailusername", mailusername);

                    //查找收件地址
                    String mailaddress="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        int chineseCount = 0;
                        for (char c : texti.toCharArray()) {
                            // 检查字符是否在汉字范围内
                            if (c >= '\u4e00' && c <= '\u9fff') {
                                chineseCount++;
                            }
                        }
                        // 如果汉字数量超过10个，则输出“123”
                        if (chineseCount > 10) {
                            mailaddress=texti;
                            break;
                        }


                    }

                    System.out.println("地址: " + mailaddress);
                    resultMap.put("mailaddress", mailaddress);

                    String mailmassage="德邦快递，"+"运单号:"+ydh.toString()+" "+mailusername+" "+jmphone+" "+mailaddress;
                    System.out.println("详细信息: " + mailmassage);
                    resultMap.put("mailmassage", mailmassage);
                } else {
                    System.out.println("解析失败");
                }

            }else if(containsJitu){
                //极兔快递解析
                resultMap.put("kd", "极兔");
                // 检查列表是否足够长
                if (dataList != null && dataList.size() > 12) {
                    String ydh="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        // 使用正则表达式检查字符串是否由纯数字组成
                        if (texti != null && texti.contains("JT") && texti.length() > 12 ) {
                            ydh=texti;
                            break;
                        }

                    }

                    System.out.println("运单号："+ydh);
                    resultMap.put("ydh", ydh);

                    //查找手机号
                    String  mailphone="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        // 使用正则表达式检查字符串是否由纯数字组成
                        if (texti != null  &&  Myutil.extractPhoneNumber2(texti) != null) {
                            // System.out.println("texti:"+texti);
                            mailphone=Myutil.extractPhoneNumber2(texti);
                            break;
                        }

                    }
                    System.out.println("手机号："+mailphone);
                    resultMap.put("mailphone", mailphone);

                    System.out.println("手机号："+mailphone);
                    resultMap.put("mailphone", mailphone);

                    String jmphone=mailphone;
                    int jmphoneindex=0;

                    System.out.println("加密手机号码: " + jmphone);
                    resultMap.put("jmphone", jmphone);


                    //查找收件人
                    String mailusername="";


                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        // 使用正则表达式检查字符串是否由纯数字组成
                        if (texti != null  &&  Myutil.extractPhoneNumber2(texti) != null) {
                            // System.out.println("texti:"+texti);
                            mailusername=Myutil.extractNonPhoneNumberPart(texti);
                            break;
                        }

                    }
                    System.out.println("收件人：: " + mailusername);
                    resultMap.put("mailusername", mailusername);


                    //查找收件地址
                    String mailaddress="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        int chineseCount = 0;
                        for (char c : texti.toCharArray()) {
                            // 检查字符是否在汉字范围内
                            if (c >= '\u4e00' && c <= '\u9fff') {
                                chineseCount++;
                            }
                        }
                        // 如果汉字数量超过10个，则输出“123”
                        if (chineseCount > 10) {
                            mailaddress=texti;
                            break;
                        }


                    }

                    System.out.println("地址: " + mailaddress);
                    resultMap.put("mailaddress", mailaddress);

                    String mailmassage="极兔快递，"+"运单号:"+ydh.toString()+" "+mailusername+" "+jmphone+" "+mailaddress;
                    System.out.println("详细信息: " + mailmassage);
                    resultMap.put("mailmassage", mailmassage);
                } else {
                    System.out.println("解析失败");
                }

            }else if(containsYouzheng){
                //邮政快递解析
                resultMap.put("kd", "邮政");
                // 检查列表是否足够长
                if (dataList != null && dataList.size() > 12) {
                    String ydh="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        // 使用正则表达式检查字符串是否由纯数字组成
                        if (texti != null && texti.length() > 12 ) {
                            ydh=texti;
                            break;
                        }

                    }

                    System.out.println("运单号："+ydh);
                    resultMap.put("ydh", ydh);

                    //查找手机号
                    String  mailphone="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        // 使用正则表达式检查字符串是否由纯数字组成
                        if (texti != null  &&  Myutil.extractPhoneNumber2(texti) != null) {
                            // System.out.println("texti:"+texti);
                            mailphone=Myutil.extractPhoneNumber2(texti);
                            break;
                        }

                    }
                    System.out.println("手机号："+mailphone);
                    resultMap.put("mailphone", mailphone);

                    System.out.println("手机号："+mailphone);
                    resultMap.put("mailphone", mailphone);

                    String jmphone=mailphone;
                    int jmphoneindex=0;

                    System.out.println("加密手机号码: " + jmphone);
                    resultMap.put("jmphone", jmphone);


                    //查找收件人
                    String mailusername="";


                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        // 使用正则表达式检查字符串是否由纯数字组成
                        if (texti != null  &&  Myutil.extractPhoneNumber2(texti) != null) {
                            // System.out.println("texti:"+texti);
                            mailusername=Myutil.extractNonPhoneNumberPart(texti);
                            break;
                        }

                    }
                    System.out.println("收件人：: " + mailusername);
                    resultMap.put("mailusername", mailusername);


                    //查找收件地址
                    String mailaddress="";
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<?, ?> datai = (Map<?, ?>) dataList.get(i);
                        // 提取 text 值
                        String texti = datai.get("text").toString();

                        int chineseCount = 0;
                        for (char c : texti.toCharArray()) {
                            // 检查字符是否在汉字范围内
                            if (c >= '\u4e00' && c <= '\u9fff') {
                                chineseCount++;
                            }
                        }
                        // 如果汉字数量超过10个，则输出“123”
                        if (chineseCount > 10) {
                            mailaddress=texti;
                            break;
                        }


                    }

                    System.out.println("地址: " + mailaddress);
                    resultMap.put("mailaddress", mailaddress);

                    String mailmassage="邮政快递，"+"运单号:"+ydh.toString()+" "+mailusername+" "+jmphone+" "+mailaddress;
                    System.out.println("详细信息: " + mailmassage);
                    resultMap.put("mailmassage", mailmassage);
                } else {
                    System.out.println("解析失败");
                }

            }else{

            }




        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return ResponseEntity.ok(resultMap);
    }

}
